package com.transaction.manage.service;

import com.transaction.common.base.PageInfo;
import com.transaction.common.util.PageUtil;
import com.transaction.manage.localMapper.FlowInfoMapper;
import com.transaction.manage.model.dto.FlowInfo;
import com.transaction.manage.model.vo.ComboboxVo;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SystemService {

    @Autowired
    private FlowInfoMapper flowInfoMapper;

    @Autowired
    private ComboboxService comboboxService;

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    public List<FlowInfo> selectFlowInfoList(FlowInfo flowInfo, PageInfo pageInfo) {
        PageUtil.StartPage(pageInfo);
        return flowInfoMapper.selectFlowInfoList(flowInfo);
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    public Map<String, Object> selectFlowInfoModalAllCombobox() {
        Map<String, Object> map = new HashedMap<>();
        //流程编码
        List<ComboboxVo> flowCodes = comboboxService.selectAllChildrenByDictKey("flowCode");

        //文件类型
        List<ComboboxVo> refFileTypes = comboboxService.selectAllChildrenByDictKey("flowCode");

        map.put("flowCodes", flowCodes);
        map.put("refFileTypes",refFileTypes);


        return map;
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void saveFlowInfo(FlowInfo flowInfo) {
        Integer id = flowInfo.getId();
        String flowCode = flowInfo.getFlowCode();
        Integer stepCode = flowInfo.getStepCode();

        if (StringUtils.isEmpty(flowCode) || stepCode == null ) {
            throw new RuntimeException("流程编码/步骤编号不允许为空");
        }

        //如果是新增
        if (id == null) {
            //判断该流程该步骤是否已存在，存在，就直接修改,不存在，插入
            FlowInfo oldFlowInfo = flowInfoMapper.selectFlowInfoByFlowCodeAndStepCode(flowCode, stepCode);
            if(oldFlowInfo != null){
                flowInfoMapper.updateFlowInfoByFlowCodeAndStepCode(flowInfo);
            }else{
                flowInfoMapper.insertFlowInfo(flowInfo);
            }
        }else{
            //如果是修改
            FlowInfo oldFlowInfo = flowInfoMapper.selectFlowInfoByFlowCodeAndStepCode(flowCode, stepCode);
            if(oldFlowInfo != null){//修改之后的流程步骤可以查出步骤
                //如果查出的流程步骤就是修改的这条
                if(id == oldFlowInfo.getId()){
                    //根据id更新成最新的流程步骤信息
                    flowInfoMapper.updateFlowInfo(flowInfo);
                }else{
                    //抛出异常，存在相同流程步骤存在
                    throw new RuntimeException("流程编码:"+flowCode+",步骤编号:"+stepCode+"的步骤已存在,不允许修改成相同的步骤");
                }
            }else{
                //根据id更新成最新的流程步骤
                flowInfoMapper.updateFlowInfo(flowInfo);
            }
        }
    }
}
